package com.guoguo.sincemusic.ui

import android.app.ProgressDialog.show
import android.content.Intent
import android.graphics.Color
import android.graphics.PorterDuff
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.guoguo.sincemusic.R
import com.guoguo.sincemusic.databinding.ActivityStepperBinding
import com.guoguo.sincemusic.ui.common.BaseActivity
import com.guoguo.sincemusic.ui.login.LoginActivity
import com.guoguo.sincemusic.utils.Tools

/**
 * 用户引导页
 * @author guoguo
 * @date 2022/2/17
 */
class StepperActivity : BaseActivity() {
    private val MAX_STEP: Int = 4
    private val aboutTitleArray = arrayListOf(
        "Ready to Travel",
        "Pick the Ticket",
        "Flight to Destination",
        "Enjoy Holiday"
    )
    private val aboutDescriptionArray = arrayListOf(
        "Choose your destination, plan Your trip. Pick the best place for Your holiday",
        "Select the day, pick Your ticket. We give you the best prices. We guarantee!",
        "Safe and Comfort flight is our priority. Professional crew and services.",
        "Enjoy your holiday, Dont forget to feel the moment and take a photo!"
    )
    private val aboutImagesArray = arrayListOf(
        R.drawable.img_wizard_1,
        R.drawable.img_wizard_2,
        R.drawable.img_wizard_3,
        R.drawable.img_wizard_4
    )
    private lateinit var binding: ActivityStepperBinding
    private lateinit var viewPager: ViewPager
    private lateinit var btnNext: Button
    override fun initBinding() {
        super.initBinding()
        binding = DataBindingUtil.setContentView(this, R.layout.activity_stepper)
    }

    override fun initView() {
        super.initView()
        viewPager = binding.viewPager
        btnNext = binding.btnNext
        viewPager.adapter = MyViewPagerAdapter()
    }

    override fun initBar() {
        super.initBar()
        Tools.setSystemBarColor(this, android.R.color.white)
        Tools.setSystemBarLight(this)
    }

    override fun initListener() {
        super.initListener()
        val intent = Intent(this,LoginActivity::class.java)
        viewPager.addOnPageChangeListener(viewPagerPageChangeListener)
        btnNext.setOnClickListener {
            val current: Int = viewPager.currentItem + 1
            if (current < MAX_STEP) {
                viewPager.currentItem = current
            } else startActivity(intent)
        }
        binding.btClose.setOnClickListener {
            startActivity(intent)
        }
    }

    private fun bottomProgressDots(current_index: Int) {
        val dotsLayout = findViewById<View>(R.id.layoutDots) as LinearLayout
        val dots:ArrayList<ImageView> = arrayListOf()
        dotsLayout.removeAllViews()
        for (i in dots.indices) {
            dots[i] = ImageView(this)
            val width_height = 15
            val params =
                LinearLayout.LayoutParams(ViewGroup.LayoutParams(width_height, width_height))
            params.setMargins(10, 10, 10, 10)
            dots[i].layoutParams = params
            dots[i].setImageResource(R.drawable.shape_circle)
            dots[i].setColorFilter(resources.getColor(R.color.grey_20), PorterDuff.Mode.SRC_IN)
            dotsLayout.addView(dots[i])
        }
        if (dots.size > 0) {
            dots[current_index].setImageResource(R.drawable.shape_circle)
            dots[current_index].setColorFilter(
                resources.getColor(R.color.orange_400),
                PorterDuff.Mode.SRC_IN
            )
        }
    }


    //  viewpager change listener
    var viewPagerPageChangeListener = object : OnPageChangeListener {
        override fun onPageSelected(position: Int) {
            bottomProgressDots(position)
            if (position == aboutTitleArray.size - 1) {
                btnNext.text = "立即开始"
                btnNext.setBackgroundColor(resources.getColor(R.color.orange_400))
                btnNext.setTextColor(Color.WHITE)
            } else {
                btnNext.text = resources.getString(R.string.next)
                btnNext.setBackgroundColor(resources.getColor(R.color.grey_10))
                btnNext.setTextColor(resources.getColor(R.color.grey_90))
            }
        }

        override fun onPageScrolled(arg0: Int, arg1: Float, arg2: Int) {}
        override fun onPageScrollStateChanged(arg0: Int) {}
    }

    /**
     * View pager adapter
     */
    inner class MyViewPagerAdapter : PagerAdapter() {
        private var layoutInflater: LayoutInflater? = null
        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            layoutInflater = getSystemService(LAYOUT_INFLATER_SERVICE) as LayoutInflater
            val view: View =
                layoutInflater!!.inflate(R.layout.item_stepper_card, container, false)
            view.apply {
                findViewById<TextView>(R.id.title).text = aboutTitleArray[position]
                findViewById<TextView>(R.id.description).text = aboutDescriptionArray[position]
                findViewById<ImageView>(R.id.image).setImageResource(aboutImagesArray[position])
            }

            container.addView(view)
            return view
        }

        override fun getCount() = aboutTitleArray.size

        override fun isViewFromObject(view: View, obj: Any): Boolean {
            return view === obj
        }

        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
            val view = `object` as View
            container.removeView(view)
        }
    }
}